<template>
  <div class="ele-body">
    <el-card shadow="never" body-style="padding:22px 22px 0 22px;">
      <!-- 搜索表单 -->
      <el-form
        ref="searchForm"
        :model="table.where"
        label-width="120px"
        @keyup.enter.native="$refs.table.reload({page:{page:1,limit:10}})"
        @submit.native.prevent
      >
        <el-row>
          <el-col :md="8" :sm="12">
            <el-form-item label="关键字:" prop="name">
              <el-input
                v-model="table.where.key"
                placeholder="请输入"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :md="8" :sm="12">
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="$refs.table.reload({page:{page:1,limit:10}})"
              >查询
              </el-button>
              <el-button
                @click="(table.where = {}) && $refs.table.reload()"
              >重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card shadow="never">
      <!-- 表格工具栏 -->
      <div class="ele-table-tool">
        <div class="ele-table-tool-title">用户列表</div>
        <div class="ele-table-tool-right">
          <el-button
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="showEdit = true"
          >添加
          </el-button>
        </div>
      </div>
      <!-- 数据表格 -->
      <ele-data-table ref="table" :config="table" :choose.sync="choose" highlight-current-row>
        <template slot-scope="{}">
          <el-table-column
            prop="username"
            label="用户名"
            show-overflow-tooltip
            min-width="110"
          />
          <el-table-column
            prop="title"
            label="用户昵称"
            show-overflow-tooltip
            min-width="110"
          />
          <el-table-column
            prop="person"
            label="关联用户"
            show-overflow-tooltip
            min-width="135"
          />
          <el-table-column
            prop="roles"
            label="用户角色"
            show-overflow-tooltip
            min-width="135"
          />
          <el-table-column
            prop="permissionmanage"
            label="权限管理"
            show-overflow-tooltip
            min-width="135"
          />
          <el-table-column
            prop="is_active"
            label="用户状态"
            show-overflow-tooltip
            min-width="135"
          >
            <template slot-scope="{ row }">{{
              row.is_out === "False" ? "否" : "是"
            }}</template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            min-width="160"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-link
                icon="el-icon-edit"
                type="primary"
                :underline="false"
                @click="edit(scope.row)"
              >配置</el-link>
            </template>
          </el-table-column>
        </template>
      </ele-data-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <el-dialog
      :title="editForm.id ? '编辑用户' : '新建用户'"
      :visible.sync="showEdit"
      :close-on-click-modal="false"
      width="600px"
      :destroy-on-close="true"
      :lock-scroll="false"
      @closed="editForm = {}"
    >
      <el-form
        ref="editForm"
        :model="editForm"
        :rules="editRules"
        label-width="82px"
        @submit.native.prevent
      >
        <el-form-item label="用户名:" prop="username">
          <el-input
            v-model="editForm.username"
            placeholder="请输入用户名"
            clearable
          />
        </el-form-item>
        <el-form-item label="密码:" prop="password">
          <el-input
            v-model="editForm.password"
            placeholder="请输入密码"
            clearable
          />
        </el-form-item>
        <el-form-item label="昵称:" prop="title">
          <el-input
            v-model="editForm.title"
            placeholder="请输入昵称"
            clearable
          />
        </el-form-item>
        <el-form-item label="角色:" prop="roles">
          <el-select
            v-model="editForm.roles"
            placeholder="请选择角色"
            style="width: 100%"
          >
            <el-option
              v-for="(item, key) in assetType"
              :key="'type' + key"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="关联用户:" prop="person">
          <el-select
            v-model="editForm.person"
            placeholder="请选择关联用户"
            style="width: 100%"
            filterable
            remote
            reserve-keyword
            :remote-method="romoteManager"
            :loading="managerLoading"
          >
            <el-option
              v-for="item in userOption"
              :key="'manager' + item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="showEdit = false">取消</el-button>
        <el-button
          type="primary"
          :loading="editLoading"
          @click="save"
        >保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { updateAsset } from '@/api/asset'
import { getRoles, createUser } from '@/api/user'
import { getUserList } from '@/api/base'
export default {
  name: 'SystemUser',
  data() {
    return {
      table: { // 表格配置
        url: '/usermanage/user/list/',
        where: {}
      },
      editForm: {}, // 编辑弹窗数据
      choose: [],
      editRules: { // 编辑弹窗表单验证规则
        name: [
          { required: true, message: '请输入资产名称', trigger: 'blur' }
        ],
        key: [
          { required: true, message: '请输入资产标识', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择资产类型', trigger: 'blur' }
        ]
      },
      showEdit: false, // 是否显示编辑弹窗
      editLoading: false, // 编辑表单加载状态
      assetType: [], // 资产类型
      loading: false,
      managerLoading: false,
      userOption: []
    }
  },
  mounted() {
    this._getRoles()
    this._getUserList()
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          this.editLoading = true
          if (this.editForm.id) {
            const data = this.editForm
            delete data.vuln_count
            delete data.is_use
            data.parent = this.editForm.parent.toString()
            updateAsset(this.editForm.id, data).then(res => {
              this.editLoading = false
              if (res.data.code === 0) {
                this.showEdit = false
                this.$message.success('保存成功')
                this.$refs.table.reload()
              } else {
                this.$message({ type: 'error', message: res.data.msg })
              }
            })
          } else {
            createUser(this.editForm).then(res => {
              this.editLoading = false
              if (res.data.code === 0) {
                this.showEdit = false
                this.$message({ type: 'success', message: '保存成功' })
                this.$refs.table.reload()
              } else {
                this.$message({ type: 'error', message: res.data.msg })
              }
            })
          }
        } else {
          return false
        }
      })
    },
    /* 编辑 */
    edit(row) {
      this.editForm = Object.assign({}, row)
      this.showEdit = true
    },
    /* 获得角色 */
    _getRoles() {
      getRoles().then(res => {
        this.assetType = res.data.data
      })
    },
    /* 资产绑定远程搜索 */
    async remoteMethod(query) {
      if (query !== '') {
        this.loading = true
        await this._getAssetList(query)
        this.loading = false
      } else {
        await this._getAssetList()
      }
    },
    /* 获得用户列表 */
    _getUserList(key) {
      getUserList(key).then(res => {
        this.userOption = res.data.data
      })
    },
    /* 绑定用户远程搜索 */
    async romoteManager(query) {
      if (query !== '') {
        this.loading = true
        await this._getUserList(query)
        this.loading = false
      } else {
        await this._getUserList()
      }
    }
  }
}
</script>

<style scoped>

</style>
